package com.example.java_chatroom.controller;

import com.example.java_chatroom.model.Message;
import com.example.java_chatroom.model.MessageMapper;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@RestController
@RequestMapping("/message")
public class MessageController {
    @Resource
    MessageMapper messageMapper;

    @GetMapping("/getMessage")
    public List<Message> getMessaged(Integer sessionId) {

        if (sessionId == null) {
            return new ArrayList<>();
        }
        //当前返回的集合中的消息顺序是按照时间降序排序的，我们返回给前端的要按照时间进行升序排序
        List<Message> messages = messageMapper.getMessagesBySessionId(sessionId);
        //对当前集合进行逆序排序
        Collections.reverse(messages);
        return messages;
    }
}
